//
//  NSAttributedString+SJAdditon.m
//  D12支付宝初级版本
//
//  Created by 周述坚 on 16/9/16.
//  Copyright © 2016年 周述坚. All rights reserved.
//

#import "NSAttributedString+SJAdditon.h"

@implementation NSAttributedString (SJAdditon)

/// 使用图像和文本生成上下排列的属性文本
///
/// @param image      图像
/// @param imageWH    图像宽高
/// @param title      标题文字
/// @param fontSize   标题字体大小
/// @param titleColor 标题颜色
/// @param spacing    图像和标题间距
///
/// @return 属性文本
+ (instancetype)sj_imageTextWithImage:(UIImage *)image
                              imageWH:(CGFloat)imageWH
                                title:(NSString *)title
                             fontSize:(CGFloat)fontSize
                           titleColor:(UIColor *)titleColor
                              spacing:(CGFloat)spacing{
    //文本字典
    NSDictionary *titleDict = @{NSFontAttributeName :[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:titleColor};
    
    NSDictionary *spacingDict = @{NSFontAttributeName:[UIFont systemFontOfSize:spacing]};
    
    //图片文本
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = image;
    attachment.bounds = CGRectMake(0, 0, imageWH, imageWH);
    
    NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];
    
    //换行文本
    NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"\n\n" attributes:spacingDict];
    
    //按钮文字
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:title attributes:titleDict];
    
    //合并文字
    NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:imageText];
    
    [attM appendAttributedString:lineText];
    [attM appendAttributedString:text];
    
    return attM.copy;
    
    


}



@end
